function [W_new,flag] = new_weight(old_index,new_index,W,n,flag,W_new)
for i = 1:length(new_index)                                
    p1 = new_index(i);                                     
    p2 = p1+1;                                             
    if ismember(p2,new_index)                              
        w = randi([0,1]);                                  
        r = w*(0.5^n*W(p1,:)+(1-0.5^n)*W(p2,:)) + (1-w)*((1-0.5^n)*W(p1,:)+0.5^n*W(p2,:));
        flag = flag + 1;                                   
        W_new(flag,:) = r;                                 
        if flag == length(old_index)                       
            break;
        end 
    else
        continue;
    end
end

end

